package models;

import java.sql.SQLException; 

/** DB persistence **/
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.misc.RuntimeExceptionBaseDaoEnabled;
import com.j256.ormlite.table.DatabaseTable;


/**
 * Thsi method holds all the information for the current Category
 * @author Shezil Kheraj
 * @version 1.0
 */
@DatabaseTable(tableName = "categories")
public class Category extends RuntimeExceptionBaseDaoEnabled<Category, Integer> {

	//Instance Data
	@DatabaseField(generatedId = true)
	private int id;

	@DatabaseField
	private String name;

	@DatabaseField
	private String description;
	
	@DatabaseField(canBeNull = false, foreignAutoRefresh = true, foreign = true)
    private User user;
	
	@ForeignCollectionField(eager = true)
    private ForeignCollection<Task> tasks;
	
	/**
	 * Getter for the user of this category
	 * @return user
	 */
	public User getUser() {
		return user;
	}
	
	/**
	 * Setter for user
	 * @param user
	 */
	public void setUser(User user) {
		this.user = user;
	}
	
	/**
	 * Getter for all the tasks
	 * @return tasks
	 */
	public ForeignCollection<Task> getTasks() {
		return tasks;
	}
		
	// For ORMLite DAO access
	private static RuntimeExceptionDao<Category, Integer> defaultDao = null;
	public static void setDefaultDao(RuntimeExceptionDao<Category, Integer> runtimeExceptionDao) {
		defaultDao = runtimeExceptionDao;
	}
	
	/**
	 * Getter for the database access object
	 * @return defaultDao
	 */
	public static RuntimeExceptionDao<Category, Integer> getDefaultDao() {
		return defaultDao;
	}

	/**
	 * Zero-arg constructor, specifically for ORMlite
	 * @throws SQLException 
	 */
	public Category() {
		super();
		setDao((RuntimeExceptionDao<Category, Integer>) defaultDao);
	}
	
	/**
	 * Constructor for category that initializes all the variables
	 * @param name
	 * @param description
	 */
	public Category(String name, String description) {
		this();
		this.name = name;
		this.description = description;
	}
	
	/**
	 * Getter for the id of a task
	 * @return id
	 */
	public int getId() {
		return id;
	}
	
	/**
	 * Getter for name of category
	 * @return name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Sets the name of a category
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * Returns the description of a category
	 * @return description
	 */
	public String getDescription() {
		return description;
	}
	
	/**
	 * Sets a description for a category
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
	
	/**
	 * Convert category to a String object
	 * @return Stringified version
	 */
	public String toString() {
		return name;
	}
}
